package test07.instrospction;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * @Author lzp
 * @Date 2024-03-28
 */
public class test02 {
    public static void main(String[] args) throws Exception {

        //实例化对象
        Person person = Person.class.getConstructor().newInstance();

        //获取对象的属性
        PropertyDescriptor namePropertyDescriptor =
                new PropertyDescriptor("name", Person.class);

        PropertyDescriptor agePropertyDescriptor =
                new PropertyDescriptor("age", Person.class);

        //获取属性的 set 方法
        Method nameWriteMethod = namePropertyDescriptor.getWriteMethod();
        Method ageWriteMethod = agePropertyDescriptor.getWriteMethod();

        //调用 set 方法
        nameWriteMethod.invoke(person,"张三");
        ageWriteMethod.invoke(person,18);

        // 获取 get 方法
        Method nameReadMethod = namePropertyDescriptor.getReadMethod();
        Method ageReadMethod = agePropertyDescriptor.getReadMethod();

        //调用 get 方法
        Object name = nameReadMethod.invoke(person);
        Object age = ageReadMethod.invoke(person);

        System.out.println(name);
        System.out.println(age);

    }
}
